﻿using TrueSync;

namespace HEFramework
{
    public static class BlinkComponentSystem
    {
        [EntitySystem]
        public class BlinkComponentAwakeSystem : AwakeSystem<BlinkComponent>
        {
            protected override void Awake(BlinkComponent _self)
            {
            }
        }


        public static bool IsCanBlink(this BlinkComponent _self, TSVector _pos)
        {
            var ac = _self.GetParentChild<AttributeComponent>();
            var tc = _self.GetParentChild<TransformComponent>();

            if (ac == null || tc == null)
            {
                return false;
            }

            //魔法不足
            FP need = _self.GetBlinkNeedMP();
            if (ac.MP < need)
            {
                return false;
            }

            //离原点过远
            FP dis = TSVector.Distance(TSVector.zero, _pos);
            if (dis > _self.GetBlinkLimitDis())
            {
                return false;
            }

            return true;
        }

        public static void Blink(this BlinkComponent _self, TSVector _pos)
        {
            var ac = _self.GetParentChild<AttributeComponent>();
            var tc = _self.GetParentChild<TransformComponent>();
            var alc = _self.GetParentChild<AlertComponent>();
            if (ac == null || tc == null || alc == null)
            {
                return;
            }

            ac.OnHM(0, -_self.GetBlinkNeedMP(), false);
            tc.P = _pos;
            alc.Target = null;
        }

        private static FP GetBlinkNeedMP(this BlinkComponent _self)
        {
            var ac = _self.GetParentChild<AttributeComponent>();
            if (ac == null)
            {
                return 0;
            }

            return ac.X_MP.Value / 5;
        }

        private static FP GetBlinkLimitDis(this BlinkComponent _self)
        {
            return 3.8;
        }
    }
}